var e = require("../../component/WxNotificationCenter/WxNotificationCenter.js"), a = require("../../component/wxParse/wxParse.js"), t = getApp();

Page({
    data: {
        winWidth: 0,
        winHeight: 0,
        currentTab: 0,
        display: "none",
        isFirstLoad: !0,
        userAccessToken: "",
        homeImage: "",
        totalAccessCount: 0,
        avatar: "/images/avatar.png",
        name: "",
        role: "",
        price: "",
        prices: [],
        priceDesc: "",
        teamId: "",
        teamName: "",
        signature: "",
        schedules: [],
        works: [],
        workTotal: 0,
        attention: 0,
        attentionClass: "attention",
        attentionText: "关注",
        schedulePermission: -1,
        scheduleSearchResult: "该用户档期已设置为隐私，请使用档期搜索功能！",
        total: -1,
        dynamics: [],
        pageNo: 1,
        shareTitle: "“婚语”婚礼人工具",
        isMyPage: !1,
        scheduleAccurate: 0,
        scheduleAccurateText: "",
        scheduleAccurateUrl: "",
        weixinProgramShareInfo: {},
        weixinProgramCooperationShareInfo: {},
        dateSelector: void 0,
        shareAccessToken: "",
        isLogin: !1
    },
    onLoad: function(a) {
        var s = this;
        wx.getSystemInfo({
            success: function(e) {
                s.setData({
                    winWidth: e.windowWidth,
                    winHeight: e.windowHeight
                });
            }
        });
        var i = a.scene;
        i && i.length > 0 ? this.getFromWeixinProgramCodeSceneValue(i) : (this.setData({
            userAccessToken: a.userAccessToken,
            isMyPage: a.userAccessToken == t.globalData.accessToken,
            isLogin: t.util.isLogin()
        }), a.shareAccessToken && this.setData({
            shareAccessToken: a.shareAccessToken
        }), t.util.openSharePage(a), this.loadHomeData()), e.addNotification("DynamicMakeTopSuccess", this.refreshDynamic, this);
    },
    refreshDynamic: function(e) {
        this.setData({
            total: -1,
            dynamics: [],
            pageNo: 1
        }), this.loadDynamicData();
    },
    swichNav: function(e) {
        var a = this;
        if (this.data.currentTab === e.target.dataset.current) return !1;
        a.setData({
            currentTab: e.target.dataset.current
        }), (e.target.dataset.current = -1 == this.data.total) && a.loadDynamicData();
    },
    clickTeam: function(e) {
        t.util.navToTeamHome(this.data.teamId);
    },
    clickPrice: function(e) {
        var a = JSON.stringify(this.data.prices);
        wx.navigateTo({
            url: "/pages/userHome/userHomeMorePrice/userHomeMorePrice?priceDesc=" + this.data.priceDesc + "&prices=" + a
        });
    },
    bindWorkMoreTap: function() {
        wx.navigateTo({
            url: "userHomeMoreWork/userHomeMoreWork?userAccessToken=" + this.data.userAccessToken
        });
    },
    clickWork: function(e) {
        var a = e.currentTarget.dataset.dynamicid;
        wx.navigateTo({
            url: "/pages/dynamicDetail/dynamicDetail?dynamicId=" + a
        });
    },
    bindCallPhoneTap: function() {
        t.util.isLoginModal() && t.util.callPhone(this.data.userAccessToken);
    },
    bindHomeTap: function() {
        var e = t.util.isLogin() ? "/pages/schedule/schedule" : "/pages/hysquare/hysquare";
        wx.reLaunch({
            url: e
        });
    },
    didSelectDate: function(e) {
        this.data.dateSelector.hide();
        var s = this;
        t.network.post({
            url: t.url.cooperationUserSearchSchedule,
            params: {
                userAccessToken: this.data.userAccessToken,
                scheduleDate: e.detail.date,
                time: e.detail.time
            },
            before: function() {
                wx.showNavigationBarLoading(), wx.showLoading({
                    title: "正在查询"
                });
            },
            success: function(e) {
                if (wx.hideLoading(), 1 == e.data.result) {
                    var i = e.data.data.scheduleText;
                    a.wxParse("htmlText", "html", i, s, 5), s.setData({
                        scheduleSearchResult: ""
                    });
                } else t.util.showToast(e.data.message);
            },
            fail: function() {
                wx.hideLoading(), t.util.showToast("获取数据失败");
            },
            complete: function() {
                wx.hideNavigationBarLoading();
            }
        });
    },
    clickSearchSchedule: function() {
        this.data.dateSelector = this.selectComponent("#timeSelector"), this.data.dateSelector.show();
    },
    getFromWeixinProgramCodeSceneValue: function(e) {
        var a = this, s = {
            scene: e
        };
        t.network.post({
            url: t.url.GetWeixinProgramCodeSceneValue,
            params: s,
            before: function() {
                wx.showNavigationBarLoading(), wx.showLoading({
                    title: "加载中"
                });
            },
            success: function(e) {
                if (1 == e.data.result) {
                    var s = e.data.data;
                    a.setData({
                        shareAccessToken: s.shareAccessToken,
                        userAccessToken: s.userAccessToken
                    }), a.loadHomeData();
                } else t.util.showToast(e.data.message);
            },
            fail: function() {
                t.util.showToast("获取数据失败");
            },
            complete: function() {
                wx.hideNavigationBarLoading();
            }
        });
    },
    bindAttentionTap: function() {
        var e = this;
        if (t.util.isLoginModal()) {
            if (e.data.userAccessToken == t.globalData.accessToken) return void t.util.showToast("自己不能关注自己");
            1 == e.data.attention ? t.network.post({
                url: t.url.cancelAttention,
                params: {
                    attentionAccessToken: e.data.userAccessToken
                },
                before: function() {
                    wx.showLoading({
                        title: "提交数据"
                    });
                },
                success: function(a) {
                    1 == a.data.result ? (t.util.showToast("取消关注成功"), e.setData({
                        attention: 0,
                        attentionClass: "attention",
                        attentionText: "关注"
                    })) : t.util.showToast(a.data.message);
                },
                fail: function() {
                    t.util.showToast("提交数据失败");
                },
                complete: function() {
                    wx.hideLoading();
                }
            }) : t.network.post({
                url: t.url.attentionUser,
                params: {
                    attentionAccessToken: e.data.userAccessToken
                },
                before: function() {
                    wx.showLoading({
                        title: "提交数据"
                    });
                },
                success: function(a) {
                    1 == a.data.result ? (t.util.showToast("关注成功"), e.setData({
                        attention: 1,
                        attentionClass: "attentioned",
                        attentionText: "已关注"
                    })) : t.util.showToast(a.data.message);
                },
                fail: function() {
                    t.util.showToast("提交数据失败");
                },
                complete: function() {
                    wx.hideLoading();
                }
            });
        }
    },
    loadHomeData: function() {
        var e = this, a = {
            userAccessToken: this.data.userAccessToken
        };
        this.data.shareAccessToken.length > 0 && (a.shareAccessToken = this.data.shareAccessToken), 
        t.network.post({
            url: t.url.userHomeInfo,
            params: a,
            before: function() {
                wx.showNavigationBarLoading(), e.data.isFirstLoad && wx.showLoading({
                    title: "加载中"
                });
            },
            success: function(a) {
                if (1 == a.data.result) {
                    var s = a.data.data, i = a.data.data.attention, o = "attention", n = "关注";
                    1 == i && (o = "attentioned", n = "已关注");
                    var r = "", c = s.prices;
                    if (c.length > 0) {
                        var u = c[0];
                        r = u.price + "元/" + u.area, c.length > 1 && (r += ">");
                    }
                    e.setData({
                        homeImage: s.homeImage,
                        totalAccessCount: s.totalAccessCount,
                        avatar: s.avatar,
                        name: s.name,
                        price: r,
                        prices: s.prices,
                        priceDesc: s.priceDesc,
                        teamId: s.teamId,
                        teamName: s.teamName,
                        signature: s.signature,
                        schedules: s.schedules,
                        role: s.role,
                        works: s.works,
                        workTotal: s.workTotal,
                        attention: i,
                        attentionClass: o,
                        attentionText: n,
                        shareTitle: s.shareTitle,
                        scheduleAccurate: parseInt(s.scheduleAccurate),
                        scheduleAccurateText: s.scheduleAccurateText,
                        scheduleAccurateUrl: s.scheduleAccurateUrl,
                        weixinProgramShareInfo: s.weixinProgramShareInfo,
                        schedulePermission: s.schedulePermission
                    }), wx.setNavigationBarTitle({
                        title: t.util.getRoleName(s.role) + " " + s.name
                    });
                } else t.util.showToast(a.data.message);
            },
            fail: function() {
                t.util.showToast("获取数据失败");
            },
            complete: function() {
                wx.hideNavigationBarLoading(), e.data.isFirstLoad && (e.setData({
                    isFirstLoad: !1,
                    display: "flex"
                }), wx.hideLoading());
            }
        });
    },
    loadDynamicData: function() {
        var e = this, a = e.data.pageNo;
        e.data.total != e.data.dynamics.length && t.network.post({
            url: t.url.listUserHomeDynamic,
            params: {
                pageNo: a,
                userAccessToken: this.data.userAccessToken
            },
            before: function() {
                wx.showNavigationBarLoading();
            },
            success: function(s) {
                if (1 == s.data.result) {
                    var i = e.data.dynamics;
                    i = i.concat(s.data.data.dynamics), a += 1, e.setData({
                        total: s.data.data.total,
                        dynamics: i,
                        pageNo: a
                    });
                } else t.util.showToast(s.data.message);
            },
            fail: function() {
                t.util.showToast("获取数据失败");
            },
            complete: function() {
                wx.hideNavigationBarLoading();
            }
        });
    },
    bindBigImageTap: function(e) {
        for (var a = e.currentTarget.dataset.dynamicid, t = this.data.dynamics, s = 0; s < t.length; s++) {
            var i = t[s];
            a == i.dynamicId && (1 == i.images.length ? wx.previewImage({
                current: i.bigImages[0],
                urls: i.bigImages
            }) : wx.previewImage({
                current: i.bigImages[e.currentTarget.dataset.index],
                urls: i.bigImages
            }));
        }
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {
        t.log("加载更多"), 1 == this.data.currentTab && this.loadDynamicData();
    },
    onShareAppMessage: function() {
        return {
            title: this.data.weixinProgramShareInfo.shareTitle,
            path: this.data.weixinProgramShareInfo.shareUrl,
            imageUrl: this.data.weixinProgramShareInfo.shareImage,
            success: function(e) {},
            fail: function(e) {}
        };
    }
});